package com.canbot.u05.versionupdate;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;


/**
 * 通用提示弹框
 */

public class AboutHintDialog extends Dialog implements View.OnClickListener {

    private String text;

    private AnimationDrawable anim_Load;

    public AboutHintDialog(Context context, int id, String text) {
        super(context, R.style.MyDialogStyle);
        this.text = text;
        setmenuDialog(id);
    }

    private void setmenuDialog(int id) {
        switch (id) {
            case 1:
                View viewdel = LayoutInflater.from(getContext()).inflate(R.layout.dialog_about1, null);
                super.setContentView(viewdel);
                TextView text_about = (TextView) viewdel.findViewById(R.id.text_about);
                text_about.setText(text);
                Button btn_sure = (Button) viewdel.findViewById(R.id.but_sure);
                btn_sure.setOnClickListener(this);
                setCancelable(true);
                setCanceledOnTouchOutside(false);
                break;

            case 2:
                View viewde2 = LayoutInflater.from(getContext()).inflate(R.layout.dialog_about2, null);
                super.setContentView(viewde2);
                ImageButton loading = (ImageButton) viewde2.findViewById(R.id.vLoad);
                loading.setBackgroundResource(R.drawable.about_load_anim);
                anim_Load = (AnimationDrawable) loading.getBackground();
                anim_Load.start();
                TextView text_about1 = (TextView) viewde2.findViewById(R.id.text_about);
                text_about1.setText(text);
                setCancelable(true);
                setCanceledOnTouchOutside(false);
                break;
        }

    }

    @Override
    public void show() {
        Window window = getWindow();
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        super.show();
        if (anim_Load != null) {
            anim_Load.start();
        }
    }

    AboutHintOnlickListner aboutHintOnlickListner;

    public interface AboutHintOnlickListner {

        void onClick(int id);
    }

    public void setListner(AboutHintOnlickListner listner) {
        this.aboutHintOnlickListner = listner;
    }

    @Override
    public void onClick(View view) {
        if (aboutHintOnlickListner != null) {
            aboutHintOnlickListner.onClick(view.getId());
        }
    }

    //设置对话框显示
    public void setDialogWindowAttr(float width, float height) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = (int) width;//宽高可设置具体大小
        lp.height = (int) height;
        window.setAttributes(lp);
    }

}
